package com.smarthome.web.property.controller.system;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.operationservicemanager.CompanyLaundryService;
import com.smarthome.domain.bean.ShCompanyLaundry;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.domain.vo.boss.TimeRange;
import com.smarthome.web.property.utils.UserHolder;


@Controller
@RequestMapping("/companyLaundryController")
public class CompanyLaundryController {
	
	@Resource
    private CompanyLaundryService companyLaundryService;
	
	@RequestMapping("/dataGrid")
    @ResponseBody
	public DataGrid getPage(ShCompanyLaundry companyLaundry,PageHelper helper,TimeRange timeRange){
		if(timeRange.getStartGmtCreate() != null) {
			companyLaundry.addQueryData("startGmtCreate", timeRange.getStartGmtCreate());
		}
		if(timeRange.getEndGmtCreate() != null) {
			companyLaundry.addQueryData("endGmtCreate", timeRange.getEndGmtCreate());
		}
		if(timeRange.getStartGmtModify() != null) {
			companyLaundry.addQueryData("startGmtModify", timeRange.getStartGmtModify());
		}
		if(timeRange.getEndGmtModify() != null) {
			companyLaundry.addQueryData("endGmtModify", timeRange.getEndGmtModify());
		}
		
		
		return companyLaundryService.qeuryDataGrid(companyLaundry, helper);
	}
	
	/**
	 * 
	* @Title: addPage 
	* @Description: 跳转新增页面 
	* @param request
	* @return
	* String
	* @throws 
	* @author cyj
	 */
	@RequestMapping("/addPage")
    public String addPage(HttpServletRequest request) {
        return "/system/operationServiceManager/companyLaundryAdd";
	}
	
	
	/**
	 * 
	* @Title: add 
	* @Description: 新增洗衣店
	* @param companyExpress
	* @return
	* Result
	* @throws 
	* @author cyj
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Result add(ShCompanyLaundry companyLaundry) {
		if(StringUtils.isBlank(companyLaundry.getName())) {
			return Result.failure("请输入名称！");
		}
		if(StringUtils.isBlank(companyLaundry.getOwnerPeron())) {
			return Result.failure("请输入负责人名称！");
		}
		if(StringUtils.isBlank(companyLaundry.getMobile())) {
			return Result.failure("请输入联系电话！");
		}
		if(StringUtils.isBlank(companyLaundry.getAddr())) {
			return Result.failure("请输入地址！");
		}
		if(companyLaundry.getAreaId() == null) {
			return Result.failure("请选择区域！");
		}
		if(companyLaundry.getStatus() == null) {
			return Result.failure("请选择状态！");
		}
		try{
			if(!companyLaundryService.checkName(companyLaundry)) {
				return Result.failure("同一地址已有同名洗衣店！");
			}
			
			companyLaundry.setGmtCreate(new Date());
			companyLaundryService.saveOrUpdate(companyLaundry);
		} catch(Exception e){
			e.printStackTrace();
			return Result.failure(e.getMessage());
		}
		return Result.success("新增成功！");
	 }
	
	
	/**
	 * 
	* @Title: edit 
	* @Description: 修改洗衣店
	* @param companyExpress
	* @return
	* Result
	* @throws 
	* @author cyj
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Result edit(ShCompanyLaundry companyLaundry) {
		if(StringUtils.isBlank(companyLaundry.getName())) {
			return Result.failure("请输入名称！");
		}
		if(StringUtils.isBlank(companyLaundry.getOwnerPeron())) {
			return Result.failure("请输入负责人名称！");
		}
		if(StringUtils.isBlank(companyLaundry.getMobile())) {
			return Result.failure("请输入联系电话！");
		}
		if(StringUtils.isBlank(companyLaundry.getAddr())) {
			return Result.failure("请输入地址！");
		}
		if(companyLaundry.getAreaId() == null) {
			return Result.failure("请选择区域！");
		}
		if(companyLaundry.getStatus() == null) {
			return Result.failure("请选择状态！");
		}
		try{
			if(!companyLaundryService.checkName(companyLaundry)) {
				return Result.failure("同一地址已有同名洗衣店！");
			}
			
			ShCompanyLaundry laundry = companyLaundryService.getById(companyLaundry.getId());

			BeanUtils.copyProperties(companyLaundry, laundry);
			
			laundry.setGmtModify(new Date());
			
			
			companyLaundryService.saveOrUpdate(laundry);
		} catch(Exception e){
			e.printStackTrace();
			return Result.failure(e.getMessage());
			  
		}
		return Result.success("修改成功！");
	 }
		
	  
	/**
	 * 
	* @Title: editPage 
	* @Description: 跳转修改页面 
	* @param request
	* @param id
	* @return
	* String
	* @throws 
	* @author cyj
	 */
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request,Long id){
		request.setAttribute("companyLaundry", companyLaundryService.getById(id));
		return "/system/operationServiceManager/companyLaundryEdit";
	}
	
	
	  
	  
	 /**
	  * 
	 * @Title: delete 
	 * @Description: 根据id删除洗衣店
	 * @param id
	 * @return
	 * Result
	 * @throws 
	 * @author cyj
	  */
	  @RequestMapping("/delete")
	  @ResponseBody
	  public Result delete(Long id) {
		  try{
			  companyLaundryService.delete(id);
		  } catch(Exception e){
			  return Result.failure("洗衣店被引用，不能删除");
		  }
		  return Result.success("删除成功！");
	  }
	  
	  
	  /**
	   * 
	  * @Title: getCombobox 
	  * @Description: 根据小区id获取洗衣店combobox
	  * @param residenceId
	  * @return
	  * List<Combobox>
	  * @throws 
	  * @author cyj
	   */
	  @RequestMapping("/getCombobox")
	  @ResponseBody
	  public List<Combobox> getCombobox(Long residenceId) {
		  if(residenceId == null && !UserHolder.isSuperResidence()) {
			  residenceId = UserHolder.getCurrentResidenceId();
		  }
		  return companyLaundryService.getCombobox(residenceId);
	  }
	  
	  
	  
	  @RequestMapping("/feePage")
	  public String feePage(HttpServletRequest request,Long id){
		  request.setAttribute("companyLaundryId", id);
		  return "/system/operationServiceManager/laundryFee";
	  }
}
